package com.pt.controller;

import com.pt.common.CommonResult;
import com.pt.entity.Checkin;
import com.pt.entity.User;
import com.pt.service.CheckinService;
import com.pt.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "签到管理")
@RestController
@RequestMapping("/api/checkin")
@RequiredArgsConstructor
public class CheckinController {
    
    private final CheckinService checkinService;
    private final UserService userService;
    
    @ApiOperation("每日签到")
    @PostMapping
    @PreAuthorize("hasRole('USER')")
    public CommonResult<Checkin> checkin(HttpServletRequest request) {
        User user = userService.getCurrentUser();
        
        try {
            Checkin checkin = checkinService.checkin(user.getId());
            return CommonResult.success(checkin);
        } catch (RuntimeException e) {
            return CommonResult.error(e.getMessage());
        }
    }
    
    @ApiOperation("检查今日是否已签到")
    @GetMapping("/status")
    @PreAuthorize("hasRole('USER')")
    public CommonResult<Boolean> getCheckinStatus(HttpServletRequest request) {
        User user = userService.getCurrentUser();
        
        boolean hasCheckedIn = checkinService.hasCheckedInToday(user.getId());
        return CommonResult.success(hasCheckedIn);
    }
    
    @ApiOperation("获取连续签到天数")
    @GetMapping("/streak")
    @PreAuthorize("hasRole('USER')")
    public CommonResult<Integer> getCheckinStreak(HttpServletRequest request) {
        User user = userService.getCurrentUser();
        
        Integer streak = checkinService.getCheckinStreak(user.getId());
        return CommonResult.success(streak);
    }
} 